import { Global, Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { AuthService } from './auth.service';
import { AdminJwtStrategy, AppJwtStrategy } from './strategies/jwt.strategy';
import { IRefreshTokenRepository, REFRESH_TOKEN_REPOSITORY } from './interfaces/refresh-token.interface';
import { InMemoryRefreshTokenRepository, RedisRefreshTokenRepository } from './repositories/refresh-token.repository';
import { TokenRefreshMiddleware } from './middleware/token-refresh.middleware';
import { AuthController } from './controllers/auth.controller';
import { RedisClientModule } from '../redis/redis.module';
import { MongooseModule } from '@nestjs/mongoose';
import { AdminUserSchema } from 'src/endpoints/admin/admin-users/entities/admin-user.entity';
import { UserSchema } from 'src/endpoints/app/users/entities/user.schema';

@Global()
@Module({
	imports: [
		PassportModule,
		JwtModule.register({}),
		RedisClientModule,
		MongooseModule.forFeature([
			AdminUserSchema,
			UserSchema
		])
	],
	controllers: [AuthController],
	providers: [
		AuthService,
		AppJwtStrategy,
		AdminJwtStrategy,
		TokenRefreshMiddleware,
		{
			provide: REFRESH_TOKEN_REPOSITORY,
			// useClass: RedisRefreshTokenRepository, 使用Redis存储refresh token
			useClass: InMemoryRefreshTokenRepository, // 使用内存存储refresh token
		},
	],
	exports: [AuthService, TokenRefreshMiddleware],
})
export class AuthModule { }
